package com.jomoo.card.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.smartcardio.CardException;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jomoo.card.dao.CardDao;
import com.jomoo.card.service.CardService;
import com.jomoo.common.pojo.JoMooCard;
import com.jomoo.common.pojo.JomooAccount;
import com.jomoo.common.tools.DateTools;
import com.jomoo.common.utils.Constants;

/**
 * 
* @ClassName: CardServiceImpl 
* @Description: 下拉菜单逻辑接口实现 
* @author sddm.haosc
* @date 2017年12月12日 上午10:12:13 
*
 */
@Service("cardServiceImpl")
@Transactional(rollbackFor = CardException.class)
public class CardServiceImpl implements CardService
{
	@Resource
	public CardDao cardDao;
	
	/**
	 * 获取产品价格
	 */
	@Override
	public Float getSecondCity(Integer iSPecid) throws CardException
	{
		Float fSale = null;

		try
		{
			fSale = cardDao.getSecondCity(iSPecid);
		}
		catch (Exception e)
		{
			new CardException("获取产品价格异常：" + e.getMessage());
		}
		return fSale;
	}

	/**
	 * 服务卡录入
	 */
	@Override
	public Integer addServiceCard(String strPopName, String strPopPhone,
			String strPopAddress, String strSetupTime, Integer iProduct,
			Integer iProSpec, Integer iFirstCity, String strSecondCity,
			Integer iSetupType, Float iMoney, Integer iIsCard,
			Integer iCustomerId, Integer iCardFlag) throws CardException
	{
		String strAddressTotal = new String();

		// 时间格式化
		SimpleDateFormat simpl = new SimpleDateFormat(Constants.DATE_TIME_PATTERN);
		
		Integer iAcId = null;
		
		Integer iId = null;

		try
		{
			// 先进行一级城市名查询操作
			String strFirstCity = cardDao.getFirstCityName(iFirstCity);

			// 拼接完整客户地址
			if (StringUtils.isNotEmpty(strFirstCity))
			{
				strAddressTotal = strFirstCity + "." + strSecondCity + "."
						+ strPopAddress;
			}
			else
			{
				strAddressTotal = strSecondCity + "." + strPopAddress;
			}

			if(1 == iCardFlag)
			{
				JoMooCard card = new JoMooCard();
				
				// 构建服务卡对象并存入值
				card.setPopName(strPopName);
				card.setPopPhone(strPopPhone);
				card.setTypeId(iProduct);
				card.setSpecId(iProSpec);
				card.setPopAddress(strAddressTotal);
				card.setSetupType(iSetupType);
				card.setActive(1);
				card.setSetupTime(simpl.parse(strSetupTime));
				card.setIsCard(iIsCard);
				card.setSale(iMoney);
				card.setSetupState(2);
				card.setCustomertype(iCustomerId);
				card.setProductNum(1);
				
				iId = cardDao.saveAllObj(card);
			}
			
			
			if(null != iId)
			{
				JomooAccount account = new JomooAccount();
				
				account.setAccountType(0);
				account.setAccountTime(simpl.parse(strSetupTime));
				account.setAccountSale(iMoney);
				account.setCardId(iId);
				account.setActive(1);
				account.setIsBalance(0);
				account.setTypeId(iProduct);
				account.setSpecId(iProSpec);
				
				iAcId = cardDao.saveAllObj(account);
			}
			
		}
		catch (Exception e)
		{
			e.printStackTrace();
			new CardException("获取产品价格异常：" + e.getMessage());
		}
		
		return iAcId;
	}

	/**
	 * 查询服务卡
	 */
	@Override
	public List<JoMooCard> getServiceCards(String strPopName, String strPopPhone,
			Integer iCustomerId, Integer iSetupType, Integer iSetupState,
			Integer iProductType, Integer iProductSpec, String strBeginTime,
			String strEndTime, Integer iFlag) throws CardException
	{
		List<JoMooCard> cardList = new ArrayList<JoMooCard>();

		try
		{
			
			Date newDate = DateTools.getAfterDay(strEndTime);
			
			if(null != iFlag)
			{
				cardList = cardDao.getServiceCardForExcle(strPopName, strPopPhone, iCustomerId, 
																iSetupType, iSetupState, iProductType, 
																	iProductSpec, strBeginTime, newDate);
			}
			else
			{
				cardList = cardDao.getServiceCards(strPopName, strPopPhone,
						iCustomerId, iSetupType, iSetupState, iProductType,
						iProductSpec, strBeginTime, newDate);
			}
		}
		catch (Exception e)
		{
			new CardException("查询服务卡异常：" + e.getMessage());
		}
		return cardList;
	}
	
	/**
	 * 作废服务卡
	 */
	@Override
	public Boolean delServiceCard(Integer iCardId) throws CardException
	{
		Boolean bFlag = false;
		
		try
		{
			if(cardDao.getAccountByCardId(iCardId) > 0)
			{
				cardDao.delServiceCard(iCardId);
				
				cardDao.delServiceCardAccount(iCardId);
				
				bFlag = true;
			}
		}
		catch (Exception e)
		{
			new CardException("作废服务卡异常：" + e.getMessage());
		}
		return bFlag;
	}
	
	/**
	 * 变更服务卡状态
	 */
	@Override
	public Integer updateCardState(Integer iCardId, Integer iFlag, String strRemark, 
														String strLazyTime, String strRevalue) throws CardException
	{
		Integer iCount = null;
		Date lazyDate = null;
		
		try
		{
			if(Constants.CARD_STATUS1.equals(iFlag))
			{
				strRemark = new SimpleDateFormat(Constants.DATE_TIME_CN).format(new Date()) +"状态变更为已成功安装";
				iCount = cardDao.updateCardState(iCardId, 1, strRemark, lazyDate);
			}
			
			if(Constants.CARD_STATUS2.equals(iFlag))
			{
				iCount = cardDao.updateCardState(iCardId, 1, "", lazyDate);
			}
			
			if(Constants.CARD_STATUS3.equals(iFlag))
			{
				if(Constants.CHANGE_STATE1.equals(strRevalue))
				{
					iCount = cardDao.updateCardState(iCardId, 0, strRemark, lazyDate);
				}
				
				if(Constants.CHANGE_STATE2.equals(strRevalue))
				{
					lazyDate = new SimpleDateFormat(Constants.DATE_TIME_PATTERN).parse(strLazyTime);
					
					iCount = cardDao.updateCardState(iCardId, 3, strRemark, lazyDate);
				}
				
			}
			
		}
		catch (Exception e)
		{
			new CardException("服务卡状态变更异常：" + e.getMessage());
		}
		return iCount;
	}
	
	/**
	 * 服务卡详情
	 */
	@Override
	public List<JoMooCard> getCardViews(Integer iCardId) throws CardException
	{
		List<JoMooCard> cardList = null;

		try
		{
			cardList = cardDao.getCardViews(iCardId);
		}
		catch (Exception e)
		{
			new CardException("获取服务卡详情异常：" + e.getMessage());
		}
		return cardList;
	}
}
